import { recordCpuSecend, showInfoPos, showInfoSwitch } from "../config"

const showInfo = {
    run(params) {
        let { room, roomName, roomMemory, owner } = params
        if (owner > 1) return  // 不展示非属于我的建筑物
        if (!room || !roomMemory['structure']) return
        Object.keys(showInfoSwitch).map(structureType => {
            if (!showInfoSwitch[structureType]) return
            switch (structureType) {
                case 'source':
                    sourceInfo(room.visual, roomMemory, structureType)
                    break;
                case 'controller':
                    controllerInfo(room.visual, roomMemory, structureType)
                    break;
                case 'spawn':
                    spawnInfo(room.visual, roomMemory, structureType)
                    break;
                case 'room':
                    roomInfo(room, room.visual, roomMemory, structureType)
                    break;
                case 'path':
                    showAllCreepPath(roomName)
                    break;
                default:
                    break;
            }
        })
    }
}
function sourceInfo(visual, roomMemory, structureType) {
    if (!roomMemory['structure']['source']) return
    Object.keys(roomMemory['structure']['source']).map(id => {
        let obj = Game.getObjectById(id)
        let objMemory = roomMemory['structure'][obj.structureType][obj.id]
        let infoPos = new RoomPosition(obj.pos.x, obj.pos.y - 1, obj.pos.roomName)
        infoPos.y += showInfoPos[structureType].y
        infoPos.x += showInfoPos[structureType].x
        visual.text(`id:${id}`, infoPos, { fontSize: 0.1, align: 'center' })
        infoPos.y += 1
        visual.text(`挂载的creep:${objMemory.creeps.join()}`, infoPos, { fontSize: 0.1, align: 'center' })
        infoPos.y += 1
        visual.text(`是否可采集:${objMemory.acceptCreep},可采集数量:${obj.energy},再生时间:${!obj.ticksToRegeneration ? 0 : obj.ticksToRegeneration}`, infoPos, { fontSize: 0.1, align: 'center' })
    })

}
function controllerInfo(visual, roomMemory, structureType) {
    if (!roomMemory['structure']['controller']) return
    let id = Object.keys(roomMemory['structure']['controller'])[0]
    let obj = Game.getObjectById(id)
    let objMemory = roomMemory['structure'][obj.structureType][obj.id]
    let infoPos = new RoomPosition(obj.pos.x, obj.pos.y - 1, obj.pos.roomName)
    infoPos.y += showInfoPos[structureType].y
    infoPos.x += showInfoPos[structureType].x
    visual.text(`id:${id}`, infoPos, { fontSize: 0.1, align: 'center' })
    infoPos.y += 1
    visual.text(`升级进度:${Math.floor((obj.progress / obj.progressTotal) * 10000) / 100}%,当前进度${obj.progress},升级总进度:${obj.progressTotal},`, infoPos, { fontSize: 0.1, align: 'center' })
    infoPos.y += 1
    visual.text(`当前等级${obj.level}` + `安全模式${!obj.safeMode ? "" : "持续时间:" + obj.safeMode + ","}剩余次数:${obj.safeModeAvailable},冷却时间:${!obj.safeModeCooldown ? 0 : obj.safeModeCooldown}`, infoPos, { fontSize: 0.1, align: 'center' })
    infoPos.y += 1
    visual.text(`降级时间:${obj.ticksToDowngrade},攻击恢复时间:${!obj.upgradeBlocked ? 0 : obj.upgradeBlocked},HP:${!obj.hits ? '*' : `${obj.hits}/${obj.hitsMax}`}`, infoPos, { fontSize: 0.1, align: 'center' })
    infoPos.y += 1
    visual.text(`挂载的creep:${objMemory.creeps.join()}`, infoPos, { fontSize: 0.1, align: 'center' })


}
function spawnInfo(visual, roomMemory, structureType) {
    if (!roomMemory['structure']['spawn']) return
    Object.keys(roomMemory['structure']['spawn']).map(id => {
        let obj = Game.getObjectById(id)
        let infoPos = new RoomPosition(obj.pos.x, obj.pos.y - 1, obj.pos.roomName)
        infoPos.y += showInfoPos[structureType].y
        infoPos.x += showInfoPos[structureType].x
        visual.text(`id:${id}`, infoPos, { fontSize: 0.1, align: 'center' })
        infoPos.y += 1
        visual.text(`生命:${obj.hits}/${obj.hitsMax},能量:${obj.store[RESOURCE_ENERGY]}`, infoPos, { fontSize: 0.1, align: 'center' })
    })
}
function roomInfo(room, visual, roomMemory, structureType) {
    if (!Memory.gameControl) Memory.gameControl = {}
    if (!Memory.gameControl.cpuUsed) Memory.gameControl.cpuUsed = []
    let cpuMemory = Memory.gameControl.cpuUsed
    cpuMemory.push(Game.cpu.getUsed())
    if (cpuMemory.length > recordCpuSecend) cpuMemory.shift()
    let infoPos = new RoomPosition(24, 1, room.name)
    infoPos.y += showInfoPos[structureType].y
    infoPos.x += showInfoPos[structureType].x
    let cpuUsed = Math.floor(Game.cpu.getUsed() * 1000) / 1000
    let cpuAve = Math.floor((cpuMemory.reduce((s, c) => s + c, 0) / cpuMemory.length) * 1000) / 1000
    visual.text(`当前cpu使用情况:${cpuUsed},近${recordCpuSecend}秒平均使用${cpuAve}`, infoPos, { fontSize: 0.1, align: 'center' })
    infoPos.y += 1
}

function showAllCreepPath(roomName) {
    let room = Game.rooms[roomName]
    Object.keys(Memory.creeps).forEach(creepName => {
        if (!Memory.creeps[creepName]['state'].path || Memory.creeps[creepName]['state'].path.length == 0) return
        // if (Memory.creeps[creepName]['state'].path[0].roomName != roomName) return
        let path = Memory.creeps[creepName]['state'].path
        if (typeof (path) === 'string') path = Room.deserializePath(path)
        room.visual.poly(path, {
            fill: 'transparent',
            stroke: '#fff',
            lineStyle: 'dashed',
            strokeWidth: .15,
            opacity: .1
        })
    });
}
export default showInfo